
function favicon(u){
		var counter = 0;
		var i;
		for(i=0; i<u.length; i++){
			if(u.charAt(i) == '/'){
				counter++;
			}
			if(counter >= 3){
				break;
			}
		}
		return u.substr(0, i)+"/favicon.ico";
	}

function query_children(pk){
	var list = [];
	$("bookmark").each(function(i, node){
		if($(node).attr("paren_key") == pk){
			list.push({node:node, children:[]});
		}
	});
	return list;
}

function mk_tree(tree){
	if(tree.node != ""){
		pk = $(tree.node).attr("key");
	}else{
		pk = "";
	}
	var children = query_children(pk);
	for(var i=0; i<children.length; i++){
		tree.children.push(mk_tree(children[i]));
	}
	return tree;
}

function to_html(tree){
	var i;
	if(tree.node != ""){
		var node = $(tree.node);
		s = "<li>"
		if($(tree.node).attr("is_dir") == "true"){
			s +="<div>";
            s += 	"<span class=\"dir\">" + node.attr("title")+ "</span>";
            s += 	"<a href=\"javascript:void(0)\" onclick=\"openDialog('/favorite/save/"+ node.attr("key") +"?parent="+ node.attr("paren_key") +"&is_dir="+ node.attr("is_dir") +"')\" ><img title=\"修改\" class=\"icon\" src=\"/media/images/icon_changelink.png\" /></a>";
            s += 	"<a href=\"javascript:void(0)\" onclick=\"openDialog('/favorite/save/?parent="+ node.attr("key") +"&is_dir=')\" ><img title=\"添加链接\" class=\"icon\" src=\"/media/images/icon_addlink.png\" /></a>";
            s += 	"<a href=\"javascript:void(0)\" onclick=\"openDialog('/favorite/save/?parent="+ node.attr("key") +"&is_dir=false')\" ><img title=\"添加文件夹\" class=\"icon\" src=\"/media/images/icon_adddir.png\" /></a>";
            s += 	"<a href=\"#\" onclick=\"del_confirm('/favorite/delete/"+ node.attr("key") +"?next='+location.href)\" ><img title=\"删除\" class=\"icon\" src=\"/media/images/icon_deletelink.png\" /></a>";
            s +="</div>";
		}else{
			s +="<div>";
            s += 	"<img width=\"15\" height=\"15\" src=\""+ favicon(node.attr("url")) +"\" />";
            s += 	"<a target=\"_blank\" href=\"/favorite/redirect/"+ node.attr("key") +"\" >"+ node.attr("title") +"</a>";
            s += 	"<a href=\"javascript:void(0)\" onclick=\"openDialog('/favorite/save/"+ node.attr("key") +"?parent="+ node.attr("paren_key") +"&is_dir="+ node.attr("is_dir") +"')\" ><img title=\"修改\" class=\"icon\" src=\"/media/images/icon_changelink.png\" /></a>";
            s += 	"<a href=\"#\" onclick=\"del_confirm('/favorite/delete/"+ node.attr("key") +"?next='+location.href)\" ><img title=\"删除\" class=\"icon\" src=\"/media/images/icon_deletelink.png\" /></a>";
            s +="</div>";
		}
		if(tree.children.length == 0){
			s += "";
		}else{
			s += "<ul>";
			for(i=0; i<tree.children.length; i++){
				s += to_html(tree.children[i]);
			}
			s += "</ul>"
		}
		s += "</li>"
	}else{
		if(tree.children.length == 0){
			s = "";
		}else{
			s = "<ul>";
			for(i=0; i<tree.children.length; i++){
				s += to_html(tree.children[i]);
			}
			s += "</ul>"
		}
	}
	return s
}


function search(str){
	var display = $("#display");
	var s = "";
	$("bookmark").each(function(i, node){
		var n = $(node);
		if((n.attr("title").match(str) || n.attr("url").match(str)) && n.attr("is_dir")!="true" ){
			s +="<div style=\"padding-top:5px;\">";
            s += 	"<img width=\"15\" height=\"15\" src=\""+ favicon(n.attr("url")) +"\" />";
            s += 	"<a target=\"_blank\" href=\"/favorite/redirect/"+ n.attr("key") +"\" >"+ n.attr("title") +"</a>";
            s += 	"<a href=\"javascript:void(0)\" onclick=\"openDialog('/favorite/save/"+ n.attr("key") +"?parent="+ n.attr("paren_key") +"&is_dir="+ n.attr("is_dir") +"')\" ><img title=\"修改\" class=\"icon\" src=\"/media/images/icon_changelink.png\" /></a>";
            s += 	"<a href=\"#\" onclick=\"del_confirm('/favorite/delete/"+ n.attr("key") +"?next='+location.href)\" ><img title=\"删除\" class=\"icon\" src=\"/media/images/icon_deletelink.png\" /></a>";
            s +="</div>";
		}
	});
	display.html(s);
}

var openDialog;

$(function() {
    var t = {node:"", children:[]};
	$("#tree > li").append(to_html(mk_tree(t)));
	$("#tree > li").treeview({
		control: "#treecontroler",
		collapsed: true
	});
	
	$("#id_query").keyup(function(){
		search($("#id_query").val());
	});
	search($("#id_query").val());
});